﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace SanctusSoftware.Logging.WinForms
{


    // http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html
    // http://www.codeproject.com/KB/recipes/ImageConverter.aspx
    public static class ScreenCapturer
    {

        private static Bitmap bmpScreenshot;

        private static Graphics gfxScreenshot;

        public static byte[] CaptureScreenToBytes()
        {
            return CaptureScreenToBytes(ImageFormat.Png); 
        }

        public static byte[] CaptureScreenToBytes(ImageFormat imageFormat)
        {

            bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

            // Create a graphics object from the bitmap
            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            // Take the screenshot from the upper left corner to the right bottom corner
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            // Save the screenshot to the specified path that the user has chosen
            //bmpScreenshot.Save(saveScreenshot.FileName, ImageFormat.Png);

            MemoryStream s = new MemoryStream();
            bmpScreenshot.Save(s, imageFormat);
            
            return s.ToArray();

        }
    }
}
